package cn.ddiancan.auth.service.repository;

import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.security.oauth2.server.authorization.client.JdbcRegisteredClientRepository;
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;

public class XddJdbcRegisteredClientRepository extends JdbcRegisteredClientRepository {

    /**
     * Constructs a {@code JdbcRegisteredClientRepository} using the provided parameters.
     *
     * @param jdbcOperations the JDBC operations
     */
    public XddJdbcRegisteredClientRepository(JdbcOperations jdbcOperations) {
        super(jdbcOperations);
    }

    @Override
    public void save(RegisteredClient registeredClient) {
        RegisteredClient byClientId = findByClientId(registeredClient.getClientId());
        if (byClientId != null) {
            registeredClient = RegisteredClient.from(registeredClient).id(byClientId.getId()).build();
        }
        super.save(registeredClient);
    }
}
